package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.把用户的密码加密
        String password = employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> query = new LambdaQueryWrapper<>();
        query.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(query);
        //判断emp是否为null
        if(emp==null){
            return R.error("登录失败");
        }
        if(!password.equals(emp.getPassword())){
            return R.error("登录失败");
        }
        if(emp.getStatus()==0){
            return R.error("账户已被禁用");
        }
        //登录成功后把id保存到session域中
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        log.info("接受到的数据{}",employee.toString());
        //初始值密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //获取是谁插入了这个员工信息,并插入
        //Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);

        //添加到数据库
        employeeService.save(employee);
        return R.success("添加成功");
    }


    /**
     * 分页条件查询
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(@RequestParam(value = "page",defaultValue ="1") Integer page, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize, @RequestParam(value = "name",defaultValue = "") String name){
        //查询数据
        Page pageInfo = new Page(page,pageSize);
        //设置条件
        LambdaQueryWrapper<Employee> query = new LambdaQueryWrapper<>();
        query.like(StringUtils.hasLength(name),Employee::getName,name);
        query.orderByDesc(Employee::getUpdateTime);
        //查询结果
        Page<Employee> employeePage = employeeService.page(pageInfo, query);
        System.out.println(employeePage.toString());
        return R.success(employeePage);
    }

    /**
     * 修改员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("修改失败，未找到信息");
    }


    /**
     * 修改员工账号状态
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //修改
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //获取是谁插入了这个员工信息,并插入
        //Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改用户成功");
    }

}
